package com.bosssoft.trainee.nontax.controller;

import com.bosssoft.trainee.nontax.annotation.UserLoginToken;
import com.bosssoft.trainee.nontax.common.result.CodeMsg;
import com.bosssoft.trainee.nontax.common.result.Result;
import com.bosssoft.trainee.nontax.entity.dto.UserDto;
import com.bosssoft.trainee.nontax.entity.dto.ElementTableData;
import com.bosssoft.trainee.nontax.entity.vo.UserVo;
import com.bosssoft.trainee.nontax.service.UserService;
import com.bosssoft.trainee.nontax.util.mapper.dtoVo.UserDtoVo;
import com.bosssoft.trainee.nontax.util.mapper.poDto.UserPoDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class UserController {
    @Autowired
    UserService userService;

    @Autowired
    UserPoDto userPoDto;

    @Autowired
    UserDtoVo userDtoVo;

    @RequestMapping("/user/getAll")
    @ResponseBody
    @UserLoginToken
    public Result<ElementTableData<UserVo>> getAllUserByElementTableDataTemplate() {
        List<UserVo> userVos = userService.queryByPage(UserDto.builder().build());
        return Result.success(new ElementTableData<>(userVos,userVos.size()));
    }

    @RequestMapping("/user/getAllUser")
    @ResponseBody
    @UserLoginToken
    public Result<List<UserVo>> getAllUser() {
        List<UserVo> userVos = userService.queryByPage(UserDto.builder().build());
        return Result.success(userVos);
    }

    @RequestMapping("/user/getAllByRoleId")
    @ResponseBody
    @UserLoginToken
    public Result<ElementTableData<UserVo>> getAllUserLimitedByRoleId(@RequestParam Integer roleId) {
        List<UserVo> users = userService.queryAllByRoleId(roleId);
        return Result.success(new ElementTableData<>(users, users.size()));
    }

    @RequestMapping("/user/addUser")
    @ResponseBody
    @UserLoginToken
    public Result<UserVo> addUser(@Validated @RequestBody UserVo userVo) {
        userVo = userService.insert(userDtoVo.vo2dto(userVo));
        return Result.success(userVo);
    }

    @RequestMapping("/user/deleteUser")
    @ResponseBody
    @UserLoginToken
    public Result<Object> deleteUserByUserId(@RequestParam Integer userId) {
        if (userId == null){
            return Result.error(CodeMsg.NOT_FIND_DATA);
        }
        return userService.deleteByIdSoft(userId) > 0 ?
                Result.success() :
                Result.error(CodeMsg.NOT_FIND_DATA);
    }

    @RequestMapping("/user/updateUser")
    @ResponseBody
    @UserLoginToken
    public Result<UserVo> updateUser(@RequestBody UserVo userVo) {
        userService.update(userDtoVo.vo2dto(userVo));
        return Result.success();
    }
}
